using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 02/04/2010 11:03:48
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CRespostasMultiplas
   {
       private string IDT_QST ;
       private string IDT_TIQ ;
       private string IDT_CAT ;
       private string IDT_QUE ;
       private string SEQ_OPCAO ;

	   /// <sumary>
       /// Construtor da classe respostas_multiplas
       /// <sumary>
       public CRespostasMultiplas()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Respostas_multiplas
       /// <sumary>
       public CRespostasMultiplas( string IDT_QST, string IDT_TIQ, string IDT_CAT, string IDT_QUE, string SEQ_OPCAO )
       {
          this.IDT_QST = IDT_QST;
          this.IDT_TIQ = IDT_TIQ;
          this.IDT_CAT = IDT_CAT;
          this.IDT_QUE = IDT_QUE;
          this.SEQ_OPCAO = SEQ_OPCAO;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do questionario
        /// <sumary>
        public string Idt_qst 
		{
		   get
		   {
		       return this.IDT_QST;
		   }
		   set
		   {
		       this.IDT_QST = value;
		   }
		}
     	/// <sumary>
        /// Identificador do tipo de questionario
        /// <sumary>
        public string Idt_tiq 
		{
		   get
		   {
		       return this.IDT_TIQ;
		   }
		   set
		   {
		       this.IDT_TIQ = value;
		   }
		}
     	/// <sumary>
        /// Identificador da categoria do questionario
        /// <sumary>
        public string Idt_cat 
		{
		   get
		   {
		       return this.IDT_CAT;
		   }
		   set
		   {
		       this.IDT_CAT = value;
		   }
		}
     	/// <sumary>
        /// Identificiador da questao
        /// <sumary>
        public string Idt_que 
		{
		   get
		   {
		       return this.IDT_QUE;
		   }
		   set
		   {
		       this.IDT_QUE = value;
		   }
		}
     	/// <sumary>
        /// Sequencial da opcao da questao
        /// <sumary>
        public string Seq_opcao 
		{
		   get
		   {
		       return this.SEQ_OPCAO;
		   }
		   set
		   {
		       this.SEQ_OPCAO = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Respostas_multiplas.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_RESPOSTAS_MULTIPLAS
			                  ( IDT_QST, IDT_TIQ, IDT_CAT, IDT_QUE, SEQ_OPCAO )
						   values
						      ( :IDT_QST, :IDT_TIQ, :IDT_CAT, :IDT_QUE, :SEQ_OPCAO )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_QST",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_TIQ",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_CAT",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_QUE",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("SEQ_OPCAO",OracleType.Number)); 
				
                command.Parameters["IDT_QST"].Value = this.IDT_QST;
                command.Parameters["IDT_TIQ"].Value = this.IDT_TIQ;
                command.Parameters["IDT_CAT"].Value = this.IDT_CAT;
                command.Parameters["IDT_QUE"].Value = this.IDT_QUE;
                command.Parameters["SEQ_OPCAO"].Value = this.SEQ_OPCAO;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Respostas_multiplas.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_RESPOSTAS_MULTIPLAS set 
			                   
						   where
						       IDT_QST = :IDT_QST, IDT_TIQ = :IDT_TIQ, IDT_CAT = :IDT_CAT, IDT_QUE = :IDT_QUE, SEQ_OPCAO = :SEQ_OPCAO ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_QST",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_TIQ",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_CAT",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_QUE",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("SEQ_OPCAO",OracleType.Number)); 
				
                command.Parameters["IDT_QST"].Value = this.IDT_QST;
                command.Parameters["IDT_TIQ"].Value = this.IDT_TIQ;
                command.Parameters["IDT_CAT"].Value = this.IDT_CAT;
                command.Parameters["IDT_QUE"].Value = this.IDT_QUE;
                command.Parameters["SEQ_OPCAO"].Value = this.SEQ_OPCAO;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Respostas_multiplas.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_RESPOSTAS_MULTIPLAS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_QST", DbType.Int32, this.IDT_QST));
            tab.adicionarFiltro(new Coluna("IDT_TIQ", DbType.Int32, this.IDT_TIQ));
            tab.adicionarFiltro(new Coluna("IDT_CAT", DbType.Int32, this.IDT_CAT));
            tab.adicionarFiltro(new Coluna("IDT_QUE", DbType.Int32, this.IDT_QUE));
            tab.adicionarFiltro(new Coluna("SEQ_OPCAO", DbType.Int32, this.SEQ_OPCAO));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RESPOSTAS_MULTIPLAS order by IDT_QST ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CRespostasMultiplas PesquisaporCodigo(string idt_qst, string idt_tiq, string idt_cat, string idt_que, string seq_opcao, string login)
        {
            CRespostasMultiplas obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_RESPOSTAS_MULTIPLAS 
                            WHERE IDT_QST = :IDT_QST, IDT_TIQ = :IDT_TIQ, IDT_CAT = :IDT_CAT, IDT_QUE = :IDT_QUE, SEQ_OPCAO = :SEQ_OPCAO";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_QST", DbType.String, idt_qst);
				qy.AdicionarParametro(":IDT_TIQ", DbType.String, idt_tiq);
				qy.AdicionarParametro(":IDT_CAT", DbType.String, idt_cat);
				qy.AdicionarParametro(":IDT_QUE", DbType.String, idt_que);
				qy.AdicionarParametro(":SEQ_OPCAO", DbType.String, seq_opcao);
               
				qy.Executar();
                obj = new CRespostasMultiplas( qy.ObterCampo("IDT_QST"), qy.ObterCampo("IDT_TIQ"), qy.ObterCampo("IDT_CAT"), qy.ObterCampo("IDT_QUE"), qy.ObterCampo("SEQ_OPCAO") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CRespostasMultiplas();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_QSTIDT_TIQIDT_CATIDT_QUESEQ_OPCAO() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
		
		static public DataTable ObterPorIDT_QST(string idt_qst)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RESPOSTAS_MULTIPLAS  where IDT_QST = :IDT_QST order by IDT_QST ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_QST", OracleType.Number));
                command.Parameters["IDT_QST"].Value = idt_qst;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_TIQ(string idt_tiq)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RESPOSTAS_MULTIPLAS  where IDT_TIQ = :IDT_TIQ order by IDT_QST ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_TIQ", OracleType.Number));
                command.Parameters["IDT_TIQ"].Value = idt_tiq;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_CAT(string idt_cat)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RESPOSTAS_MULTIPLAS  where IDT_CAT = :IDT_CAT order by IDT_QST ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_CAT", OracleType.Number));
                command.Parameters["IDT_CAT"].Value = idt_cat;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_QUE(string idt_que)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RESPOSTAS_MULTIPLAS  where IDT_QUE = :IDT_QUE order by IDT_QST ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_QUE", OracleType.Number));
                command.Parameters["IDT_QUE"].Value = idt_que;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorSEQ_OPCAO(string seq_opcao)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RESPOSTAS_MULTIPLAS  where SEQ_OPCAO = :SEQ_OPCAO order by IDT_QST ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("SEQ_OPCAO", OracleType.Number));
                command.Parameters["SEQ_OPCAO"].Value = seq_opcao;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
    }
}	

